package model;

/**
* Objects of this class describe a single node in a network.
*/

public class NetworkNode extends NetworkObject {
	//--------------------------------------------------------------------------------------------------------------
	//--------------------------------------------------------------------------------------------------------------
	//Variables
	private String nodeName;
	private double xCenter, yCenter;
	private NetworkModel networkModel;
	
	//--------------------------------------------------------------------------------------------------------------
	//--------------------------------------------------------------------------------------------------------------
	//Constructor
	/**
	* Creates a network node.
	* @param nodeName the name that the node will be identified by. Names are exact
	*		and case sensitive.
	* @param xCenter the X coordinate of the center of the node in pixels
	* @param yCenter the Y coordinate of the center of the node in pixels
	*/
	public NetworkNode(String nodeName, double xCenter, double yCenter) {
		this.nodeName = nodeName;
		this.xCenter = ((int)xCenter / 10) * 10;
		this.yCenter = ((int)yCenter / 10) * 10;
	}
	
	//--------------------------------------------------------------------------------------------------------------
	//--------------------------------------------------------------------------------------------------------------
	//Methods
	/**
	* @return name of the node
	*/
	public String getName() {
		return nodeName;
	}
	
	//----------------------------------------
	/**
	* Changes the name of the node.
	* @param newName
	*/
	public void setName(String newName) {
		nodeName = newName;
		networkModel.affectChange();
	}
	
	//----------------------------------------
	/**
	* @return the X coordinate of the center of the node
	*/
	public double getX() {
		return xCenter;
	}
	
	//----------------------------------------
	/**
	* @return the Y coordinate of the center of the node
	*/
	public double getY() {
		return yCenter;
	}
	
	//----------------------------------------
	/**
	* Changes the location of the center of the node.
	*/
	public void setLocation(double xCenter, double yCenter) {
		this.xCenter = Math.max(xCenter, 0);
		this.yCenter = Math.max(yCenter, 0);
		networkModel.affectChange();
	}
	
	public void finalizeLocation(double d, double e) {
		this.xCenter = Math.max(((int)xCenter / 10) * 10, 0);
		this.yCenter = Math.max(((int)yCenter / 10) * 10, 0);
		networkModel.affectChange();
	}

	//----------------------------------------
	/**
	* Sets a reference to the network model that this node belongs to.
	* @param network
	*/
	public void setNetwork(NetworkModel networkModel) {
		this.networkModel = networkModel;
	}
	
	//----------------------------------------
	/**
	* @return the network that this node belongs to
	*/
	public NetworkModel getNetwork() {
		return networkModel;
	}

	//----------------------------------------
	/**
	* @return the network that this node belongs to
	*/
	public String toString() {
		return "N " + xCenter + " " + yCenter + " \"" + nodeName + "\"";
	}

	public boolean equals(Object o) {
		assert o instanceof NetworkNode;
		return ((NetworkNode)o).getName().equals(nodeName);
	}
}